<?php

namespace App\Kuafu\Shop\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;

class ShopDistributionModel extends BaseModel
{
    use KfSoftDeletes;

    protected $connection = 'mysql';
    protected $table      = 'shop_distribution';
    protected $primaryKey = 'id';
    protected $fillable   = [
        'id',
        'shopId',
        'serialNo',
        'partnerName',
        'provinceCode',
        'cityCode',
        'countyCode',
        'detailInfo',
        'cooperationLeader',
        'mobile',
        'ratio',
        'settlementCycleType',
        'minDeliveryAmount',
        'status',
        'qrCodeUrl',
        'isDeleted',
        'updatedByUid',
        'createdByUid',
        'updatedAt',
        'createdAt',
    ];


    const CYCLE_TYPE_WEEK  = 1; //结算周期 1:周
    const CYCLE_TYPE_MONTH = 2; //结算周期 2:月

    const CYCLE_TYPE_MAPPING = [
        self::CYCLE_TYPE_WEEK  => '周',
        self::CYCLE_TYPE_MONTH => '月',
    ];
    const STATUS_DEFAULT     = 1;
    const STATUS_END         = 2;

    const STATUS_MAPPING = [
        self::STATUS_DEFAULT => '合作中',
        self::STATUS_END     => '已终止',
    ];

    public static function getCount(int $shopId, int $status): int
    {
        return ShopDistributionModel::query()
                                    ->when($shopId > 0, function ($query) use ($shopId) {
                                        $query->where("shopId", $shopId);
                                    })
                                    ->when($status, function ($query) use ($status) {
                                        $query->where("status", $status);
                                    })->count();
    }
}
